#pragma once

#include <string>
#include <vector>
#include <map>
#include <optional>
#include <nlohmann/json.hpp>
#include "base_noncopyable.h"

namespace El {
namespace Cloud {

class CloudModelAudio {
    DISALLOW_COPY_AND_MOVE(CloudModelAudio);

public:
    static CloudModelAudio &GetInstance();

    void Start();
    void Stop();

    int32_t NewConversation();
    bool DeleteConversation(int32_t conversation_id);
    std::string ProcessAudio(int32_t conversation_id, const std::string &audio_file_path, const std::string &prompt);
    std::string GenerateText(int32_t conversation_id, const std::string &prompt);

private:
    CloudModelAudio();
    ~CloudModelAudio() = default;

    bool InitConfig();
    static size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *s);
    std::string SendHttpRequest(const std::string &post_data, const std::string &url);
    std::string ParseResponse(const std::string &response);
    std::string ProcessMessage(int32_t conversation_id, const nlohmann::json &message);
    size_t CalculateAudioTokens(size_t audio_size);
    size_t CalculateTextTokens(const std::string &text);
    size_t CalculateMessageTokens(const nlohmann::json &message);

private:
    std::string api_key_;
    std::string api_url_;
    std::map<int32_t, std::vector<nlohmann::json>> conversations_;
    std::string model_audio_;
    size_t max_context_length_audio_{0};
    int32_t next_conversation_id_{0};
};

} // namespace Cloud
} // namespace El
